package com.example.zbh.dsliao.view;

import android.app.Dialog;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.provider.MediaStore;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.Volley;
import com.bumptech.glide.Glide;
import com.example.zbh.dsliao.R;
import com.example.zbh.dsliao.application.App;
import com.example.zbh.dsliao.db.entity.ImageViewpager;
import com.example.zbh.dsliao.ui.activty.CharActivity;
import com.example.zbh.dsliao.ui.adapter.LargeImageViewPagerAdapter;

import java.util.List;



/**
 * 大图片dialog
 */
public class CusLargeDialog extends Dialog implements ViewPager.OnPageChangeListener, LargeImageViewPagerAdapter.OnPageDismissClickListener {

    private static int default_width = 600;//默认宽度
    private static int default_height = 900;//默认高度
    private Context context;
    private TextView countImageTv;
    private List<ImageViewpager> list;
    private ViewPager largeViewPagerIv;
    private LargeImageViewPagerAdapter adapter;
    private int position;

    private RequestQueue queue;


    public CusLargeDialog(Context context, List<ImageViewpager> list, int position) {
        super(context, R.style.DialogTheme);
        this.context = context;
        Log.d("gedadadada", "list:" + list + ">>>>>>");
        this.list = list;
        this.position = position;
        init();
    }

    private void init() {
        //加载布局
        setContentView(R.layout.dialog_largeimage);
        countImageTv = (TextView) findViewById(R.id.countImageTv);
        countImageTv.setText((position + 1) + " / " + list.size());
        largeViewPagerIv = (ViewPager) findViewById(R.id.largeViewPagerIv);
        adapter = new LargeImageViewPagerAdapter();
        largeViewPagerIv.setAdapter(adapter);
        adapter.addData(context, list);
        adapter.setOnPageDismissClickListener(this);
        largeViewPagerIv.setCurrentItem(position);
        largeViewPagerIv.addOnPageChangeListener(this);
        queue = Volley.newRequestQueue(App.getContext());

        Log.i("taggggg", position + "sssss.......ssss..");

        //设置窗口属性
        Window window = getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        float density = getDensity(context);
        params.width = default_width;
        params.height = default_height;
        params.gravity = Gravity.CENTER;
        window.setAttributes(params);
    }




    /**
     * 获取显示密度
     * @param context
     * @return
     */
    public float getDensity(Context context) {
        Resources res = context.getResources();
        DisplayMetrics dm = res.getDisplayMetrics();
        return dm.density;
    }


    private CustomLargeDialogLisrener customLargeDialogLisrener;

    public void setCustomLargeDialogLisrener(CustomLargeDialogLisrener customLargeDialogLisrener){
        this.customLargeDialogLisrener = customLargeDialogLisrener;
    }

    /**
     * 图片长按短按事件 回调
     * **/
    @Override
    public void onDismissClick(int code) {
        if (code == 1){//短按 (关闭dialog)
            this.dismiss();
        }else if (code == 2){//弹出dialog
            final CustomChangeDialog dialog = new CustomChangeDialog(context, "您要下载本图片么?", View.GONE, "", "确定", "取消");
            dialog.show();
            dialog.setCustomChangeDialogListener(new CustomChangeDialog.CustomChangeDialogListener() {
                @Override
                public void onClick(View view) {
                    switch (view.getId()){
                        case R.id.changeTwoBtn://保存
                            ImageViewpager imageViewpager = list.get(position);
                            String url = imageViewpager.getImageUrl();
                            saveImage(url);
                            dialog.dismiss();
                            break;
                        case R.id.cancleBtn://取消
                            dialog.dismiss();
                            break;
                    }
                }
            });
        }
    }

    /**
     * 保存图片到手机相册
     **/
    private void saveImage(String url) {
        ImageRequest request = new ImageRequest(url, new Response.Listener<Bitmap>() {
            @Override
            public void onResponse(Bitmap response) {
                //图片保存到手机
                MediaStore.Images.Media.insertImage(context.getContentResolver(), response, "id", "id");
                Toast.makeText(context, "图片保存成功..快去看看吧", Toast.LENGTH_SHORT).show();
            }
        }, 1000, 1000, Bitmap.Config.ARGB_8888, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        });
        queue.add(request);
    }




    public interface CustomLargeDialogLisrener{

        void onClickLarge(View view);
    }



    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        //设置viewpager当前位置显示页
        countImageTv.setText((position + 1) + " / " + list.size());
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }


}
